package com.back.end.Service.impl;

import com.back.end.Dao.AppUserDao;
import com.back.end.Dao.OrderDao;
import com.back.end.Modal.Order;
import com.back.end.Modal.RefundOrder;
import com.back.end.Service.OrdersService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

@Service
public class OrderServiceImpl implements OrdersService {

    @Resource
    OrderDao orderDao;

    @Override
    public Order addOrder(Order order) {
        int r1 = (int) (Math.random() * (10));//产生1个0-9的随机数
        long now = System.currentTimeMillis();//一个13位的时间戳
        String customerId = String.valueOf(order.getCustomerId());
        String end = customerId.substring(customerId.length() - 2);
        String orderId = r1 + String.valueOf(now) + end;// 订单ID
        Timestamp create = new Timestamp(now);
        order.setId(orderId);
        order.setCreatedTime(create);
        orderDao.addOrder(order);
        orderDao.updateMoney(order.getPayment(),order.getCustomerId());
        return order;
    }

    @Override
    public List<Order> getOrderList(int id) {
        List<Order> orderList = orderDao.getOrderList(id);
        return orderList;
    }

    @Override
    public void finishOrder(String id) {
        Timestamp now = new Timestamp(System.currentTimeMillis());
        orderDao.finishOrder(id, now);
    }

    @Override
    public List<Order> getOrderListByPage(int pageNum, int pageSize) {
        List<Order> orders = orderDao.getAllOrderListByPage(pageNum, pageSize);
        return orders;
    }

    @Override
    public int getAllOrderCount() {
        int count = orderDao.getAllOrderCount();
        return count;
    }

    @Override
    public void refundOrderSubmit(RefundOrder refundOrder) {
        Timestamp now = new Timestamp(System.currentTimeMillis());
        refundOrder.setCreatedTime(now);
        orderDao.refundOrderSubmit(refundOrder);
        orderDao.updateOrdersRefund(refundOrder.getOrderId(), now);
    }

    @Override
    public List<RefundOrder> getAllRefundOrders() {
        List<RefundOrder> refundOrderList = orderDao.getAllRefundOrders();
        return refundOrderList;
    }

    @Override
    public List<Order> getAppRefundOrders(int id) {
        List<Order> refundOrderList = orderDao.getAppRefundOrders(id);
        return refundOrderList;
    }

    @Override
    public void confirmOrder(RefundOrder refundOrder) {
        Timestamp now = new Timestamp(System.currentTimeMillis());
        orderDao.confirmRefundOrder(refundOrder.getId(), refundOrder.getOrderId(), refundOrder.getCustomerId(), refundOrder.getRefundAmount(), now);
        orderDao.deleteRefundOrder(refundOrder.getId());
    }

}
